package com.ruoyi.regulatory.service;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.regulatory.domain.JySchoolClass;
import com.ruoyi.regulatory.mapper.JySchoolClassMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 学校班级Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-25
 */
@Service
@RequiredArgsConstructor
public class JySchoolClassService {
    private final JySchoolClassMapper jySchoolClassMapper;

    /**
     * 查询学校班级
     *
     * @param id 学校班级主键
     * @return 学校班级
     */
    public JySchoolClass selectJySchoolClassById(Long id) {
        return jySchoolClassMapper.selectJySchoolClassById(id);
    }

    /**
     * 查询学校班级列表
     *
     * @param jySchoolClass 学校班级
     * @return 学校班级
     */
    public List<JySchoolClass> selectJySchoolClassList(JySchoolClass jySchoolClass) {
        return jySchoolClassMapper.selectJySchoolClassList(jySchoolClass);
    }

    /**
     * 新增学校班级
     *
     * @param jySchoolClass 学校班级
     * @return 结果
     */
    public void insertJySchoolClass(JySchoolClass jySchoolClass) {
        String name = jySchoolClass.getName();
        String[] names = name.replaceAll(",", "，").split("，");
        jySchoolClass.setCreateTime(DateUtils.getNowDate());

        for (String v : names) {
            JySchoolClass item = BeanUtil.copyProperties(jySchoolClass, JySchoolClass.class);
            item.setName(v);

            jySchoolClassMapper.insertJySchoolClass(item);
        }
    }

    /**
     * 修改学校班级
     *
     * @param jySchoolClass 学校班级
     * @return 结果
     */
    public int updateJySchoolClass(JySchoolClass jySchoolClass) {
        jySchoolClass.setUpdateTime(DateUtils.getNowDate());
        return jySchoolClassMapper.updateJySchoolClass(jySchoolClass);
    }

    /**
     * 批量删除学校班级
     *
     * @param ids 需要删除的学校班级主键
     * @return 结果
     */
    public int deleteJySchoolClassByIds(Long[] ids) {
        return jySchoolClassMapper.deleteJySchoolClassByIds(ids);
    }

    /**
     * 删除学校班级信息
     *
     * @param id 学校班级主键
     * @return 结果
     */
    public int deleteJySchoolClassById(Long id) {
        return jySchoolClassMapper.deleteJySchoolClassById(id);
    }
}
